package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.dto.RolePermissionDto;
import com.jyy.prefabricated.entity.SysPermission;
import com.jyy.prefabricated.entity.SysRole;
import com.jyy.prefabricated.service.SysPermissionService;
import com.jyy.prefabricated.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户角色
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping("/roles")
    public AjaxResult insert(@Valid @RequestBody SysRole role){
        log.info("Rest request to insert role:{}",role);
        int id = roleService.insert(role);
        SysRole sysRole = roleService.selectById(id);
        return AjaxResult.success(sysRole);
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @PutMapping("/roles")
    public AjaxResult update(@RequestBody SysRole role){
        log.info("Rest request to update role:{}",role);
        roleService.update(role);
        SysRole sysRole = roleService.selectById(role.getId());
        return AjaxResult.success(sysRole);
    }

    /**
     * 修改角色状态
     * @param id
     * @return
     */
    @PutMapping("/roles-status/{id}")
    public AjaxResult updateStatus(@PathVariable Integer id){
        log.info("Rest request to update role status by id:{}",id);
        int row = roleService.updateStatus(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询角色详情
     * @param id
     * @return
     */
    @GetMapping("/roles/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find role by id:{}",id);
        SysRole result = roleService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/roles/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete role by id:{}",id);
        int row = roleService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询角色
     * @param status 状态
     * @param pageable
     * @return
     */
    @GetMapping("/roles")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer status, Pageable pageable){
        log.info("Rest request to find role page");
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<SysRole> roles = roleService.selectByParams(status);
        PageInfo<SysRole> result = new PageInfo<>(roles);
        return AjaxResult.success(result);
    }

    /**
     * 查询角色
     * @param status 状态
     * @return
     */
    @GetMapping("/roles-list")
    public AjaxResult selectAll(@RequestParam(required = false) Integer status){
        log.info("Rest request to find role list");
        List<SysRole> result = roleService.selectByParams(status);
        return AjaxResult.success(result);
    }

    /**
     * 查询角色权限
     * @param id
     * @return
     */
    @GetMapping("/roles-permissions/{id}")
    public AjaxResult findRolePermission(@PathVariable Integer id){
        log.info("Rest request to find role permission list");
        List<SysPermission> result = permissionService.selectRolePermissionByRoleId(id);
        return AjaxResult.success(result);
    }

    /**
     * 设置角色权限
     * @param dto
     * @return
     */
    @PutMapping("/roles-permissions")
    public AjaxResult putRolePermission(@RequestBody RolePermissionDto dto){
        log.info("Rest request to set role permission list :{}",dto);
        roleService.putPermission(dto);
        return AjaxResult.success();
    }
}
